<?php
namespace addons\member\controllers;
use addons\member\models\Member;
use addons\vote\controllers\rank;
use addons\vote\models\VoteLogModel;
use controllers\AdminApi;
use models\AuthInfo;
use models\UsersModel;
use system\Tool\Upload;

/**
 * @auth 会员管理
 * Class Index
 * @author Colin <xiongxinsheng@yikaosheng.com>
 * @date 2021-05-13 上午11:08
 * @package addons\member\controller
 */
class index extends AdminApi{
	protected $model = '';
	public function onStepUp() {
		parent::onStepUp(); // TODO: Change the autogenerated stub
		$this->model = new Member();
	}

	/**
	 * 首页接口
	 * @param string $nickname
	 * @auth 会员列表
	 * @return array
	 */
	public function index($nickname = '') {
		$model = new Member();
		$map   = [];
		if ($nickname) {
			$map['nickname'] = $nickname;
		}
		list($page, $limit) = self::getPageInfo();
		$platform = $model::Platform;
		$gender = $model::Gender;
		$state = $model::State;
		$list     = $model->field('id,name,update_time,create_time,state,platform,version,ticket,ticket_put,number,user_avatar')->where($map)->order('ticket desc')->limit($page, $limit)->select(true)->each(function (&$item) use ($platform , $gender , $state){
			$item['update_time']   = timeFormat($item['update_time']);
			$item['state']   = $state[$item['state']];
			$item['gender']   = $gender[$item['sex']];
			$item['platform']   = $platform[$item['platform']];
			$item['create_time'] = timeFormat($item['create_time']);
			$item['number'] = $this->showNumber($item['number']);
			$item['user_avatar'] = $this->getImage($item['user_avatar']);
		});

		return self::returnJson($list, $model->count($map));
	}

	/**
	 * 显示工号
	 * @param $number
	 * @return string
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-13 下午3:30
	 */
	protected function showNumber($number){
		if ($number < 10){
			return '00' . $number;
		}
		if ($number < 100){
			return '0' . $number;
		}
		return $number;
	}

	protected function onUpdateBefore(&$data = []) {
		if (!$data['user_avatar']){
			unset($data['user_avatar']);
		}else{
			if(Upload::isTmp($data['user_avatar'])){
				$to = str_replace('/tmp' , '' , $data['user_avatar']);
				Upload::moveUpload($data['user_avatar'] , $to);
				$data['user_avatar'] = $to;
			}
		}
		$data['number'] = intval($data['number']);
		$model = new VoteLogModel();
		$exists = $model->userNumberExists($data['number'] , $data['id']);
		if($exists){
			$this->error = '该工号已被占用';
			return false;
		}
		return parent::onUpdateBefore($data); // TODO: Change the autogenerated stub
	}

	protected function onUpdateAfter($status , &$data = [] , $find = []) {
		if ($status){
			if ($data['user_avatar'] && $find['user_avatar']){
				$avatar = ltrim($find['user_avatar'] , '/');
				if (file_exists($avatar)) unset($avatar);
			}
			if ($data['ticket'] > 0 && $data['ticket'] != $find['ticket']){
				rank::setScore($data['id'] , $data['ticket']);
			}
		}
		return parent::onUpdateAfter($status , $data , $find); // TODO: Change the autogenerated stub
	}

	/**
	 * 获取头像地址
	 * @param $image
	 * @return string
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-11 下午10:03
	 */
	protected function getImage($image){
		if (!$image){
			return '';
		}
		return envg('img_host') . $image;
	}

	/**
	 * 设置弹窗标题
	 * @return string
	 * @author Colin <amcolin@126.com>
	 * @date 2021-12-30 下午4:05
	 */
	protected static function setPageTitle() {
		return '会员';
	}

	/**
	 * 设置面包屑
	 * @return array
	 * @author Colin <amcolin@126.com>
	 * @date 2021-12-30 下午4:05
	 */
	protected static function setCrumbs() {
		return ['会员管理' , '会员列表'];
	}
}